package form;

import com.intellij.ide.util.PropertiesComponent;
import com.intellij.psi.codeStyle.CodeStyleSettings;
import com.intellij.psi.codeStyle.CodeStyleSettingsManager;
import org.apache.http.util.TextUtils;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.math.BigDecimal;

/**
 * 工具
 */
public final class Utils {
    // Mvvm base包的路径
    static final String MVVM_BASE_PACKAGE_PATH = "mvvm_base_package_path";
    // Mvvm base包的路径 扩展
    static final String MVVM_BASE_PACKAGE_PATH_MORE = "mvvm_base_package_path_more";

    public static final String DEFAULT_MVVM_BASE_PACKAGE_PATH = "base";
    public static final String DEFAULT_MVVM_BASE_PACKAGE_PATH_MORE = "";

    private Utils() {
    }

    /**
     * 获取自定义mvvm路径
     */
    public static String getMvvmBasePackagePath() {
        String packageName = MvvmPackageNameBean.getInstance().getPackageName();
        if (!TextUtils.isEmpty(packageName)) {
            return packageName;
        }
        return getPath(MVVM_BASE_PACKAGE_PATH);
    }

    /**
     * 设置自定义mvvm路径
     */
    public static void setMvvmBasePackagePath(String path) {
        MvvmPackageNameBean.getInstance().setPackageName(path);
        setPath(MVVM_BASE_PACKAGE_PATH, path);
    }

    /**
     * 获取自定义mvvm路径
     */
    public static String getMvvmBasePackagePathMore() {
        String packageName = MvvmPackageNameBean.getInstance().getPackageNameMore();
        if (!TextUtils.isEmpty(packageName)) {
            return packageName;
        }
        return getPath(MVVM_BASE_PACKAGE_PATH_MORE);
    }

    /**
     * 设置自定义mvvm路径
     */
    public static void setMvvmBasePackagePathMore(String path) {
        MvvmPackageNameBean.getInstance().setPackageNameMore(path);
        setPath(MVVM_BASE_PACKAGE_PATH_MORE, path);
    }

    /**
     * 设置持久化存储值
     */
    private static void setPath(String key, String path) {
        PropertiesComponent.getInstance().setValue(key, path);
    }

    /**
     * 获取持久化存储值
     */
    private static String getPath(String key) {
        if (PropertiesComponent.getInstance().isValueSet(key)) {
            return PropertiesComponent.getInstance().getValue(key, "");
        } else {
            CodeStyleSettingsManager manager = CodeStyleSettingsManager.getInstance();
            CodeStyleSettings settings = manager.getCurrentSettings();
            return settings.FIELD_NAME_PREFIX;
        }
    }

    /**
     * 生成
     *
     * @param content   类中的内容
     * @param classPath 类文件路径
     * @param className 类文件名称
     */
    public static void writeToFile(String content, String classPath, String className) {
        try {
            File floder = new File(classPath);
            if (!floder.exists()) {
                floder.mkdirs();
            }

            File file = new File(classPath + "/" + className);
            if (!file.exists()) {
                file.createNewFile();
            }

            FileWriter fw = new FileWriter(file.getAbsoluteFile());
            BufferedWriter bw = new BufferedWriter(fw);
            bw.write(content);
            bw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 加法运算
     *
     * @param m1
     * @param m2
     * @return
     */
    public static double addDouble(double m1, double m2) {
        BigDecimal p1 = new BigDecimal(Double.toString(m1));
        BigDecimal p2 = new BigDecimal(Double.toString(m2));
        return p1.add(p2).doubleValue();
    }

    /**
     * 减法运算
     *
     * @param m1
     * @param m2
     * @return
     */
    public static double subDouble(double m1, double m2) {
        BigDecimal p1 = new BigDecimal(Double.toString(m1));
        BigDecimal p2 = new BigDecimal(Double.toString(m2));
        return p1.subtract(p2).doubleValue();
    }

    /**
     * 乘法运算
     *
     * @param m1
     * @param m2
     * @return
     */
    public static double mul(double m1, double m2) {
        BigDecimal p1 = new BigDecimal(Double.toString(m1));
        BigDecimal p2 = new BigDecimal(Double.toString(m2));
        return p1.multiply(p2).doubleValue();
    }


    /**
     * 除法运算
     *
     * @param m1
     * @param m2
     * @param scale
     * @return
     */
    public static double div(double m1, double m2, int scale) {
        if (scale < 0) {
            throw new IllegalArgumentException("Parameter error");
        }
        BigDecimal p1 = new BigDecimal(Double.toString(m1));
        BigDecimal p2 = new BigDecimal(Double.toString(m2));
        return p1.divide(p2, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
    }
}